package mcweb.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;

public class DownloadView extends AbstractView {
	
	
	@Override
	public String getContentType() {
		return "application/octet-stream";
	}
	

	@Override
	protected void renderMergedOutputModel(Map map, HttpServletRequest req, HttpServletResponse res) throws Exception {
		String fileName = (String)map.get("fileName");
		String originFileName = (String)map.get("originFileName");
		System.out.println(fileName + ": " + originFileName);
		String fileDir = getServletContext().getRealPath("/upload");
		File downFile = new File(fileDir, fileName);
		
		res.setContentType(getContentType());
		res.setHeader("Content-Disposition", "attachment; fileName="+new String(originFileName.getBytes("euc-kr"), "8859_1"));
		
		OutputStream out = res.getOutputStream();
		FileInputStream in = new FileInputStream(downFile);
		
		FileCopyUtils.copy(in, out);
	}

}
